<?php

use Illuminate\Database\Seeder;
use Faker\Factory;
use App\Model\Admin\Article;
use App\Model\Admin\User;
use Illuminate\Support\Facades\Storage;

class ArticleTableSeeder extends Seeder
{

    /**
     * php artisan make:seeder ArticleTableSeeder
     *
     * php artisan db:seed --class=ArticleTableSeeder
     *
     * Run the database seeds.
     *
     * @return void
     */
    protected $ignoreFile = [
        '.gitignore',
        'placeholder.jpeg',
    ];

    protected $ignoreFolder = 'placeholder';

    public function run()
    {
        //$ids = Article::all()->modelKeys();

        $this->clearTable();

        $data = [];
        if (count(Article::all()) < 100) {
            $data = $this->data();
        } else {
            $ids = Article::all()->pluck('icon', 'id')->toArray();
            $id = array_rand($ids,1);
        }

//        $columns = \Illuminate\Support\Facades\Schema::getColumnListing('case');
//        dump($columns);

        foreach ($data as $item) {
            Article::create($item);
        }
    }

    public function data()
    {
        $i = 0;
        $data = [];
        $faker = Factory::create();
        $ids = User::all()->modelKeys();

        do {
            $case = [
                'icon' => '',
                'title' => $faker->name(),
                'content' => $faker->sentence(250),
                'is_active' => rand(0, 1),
                'is_recommend' => rand(0, 1),
                'is_original' => rand(0,2) > 0 ? 0 : 1,
                'is_approved' => rand(0, 1),
                'user_id' => $ids[array_rand($ids,1)],
                'approved_user' => $ids[array_rand($ids,1)],
            ];

            $files = $this->getImages();
            $case['folder'] = $this->formatFolder($case['name']);
            $case['icon'] = $files[rand(1,9999) % count($files)];

            $data = array_merge($data, [$case]);

            $i++;
        } while ($i < 100);

        return $data;
    }

    private function clearTable()
    {
        DB::table('article')->truncate();
    }

    private function formatFolder($name)
    {
        $name = preg_replace('/\s+/', '_', $name);
        $name = preg_replace('/\'/', '_', $name);
        $name = preg_replace('/`/', '_', $name);
        $name = preg_replace('/"/', '_', $name);
        $name = preg_replace('/\\\\/', '_', $name);
        $name = preg_replace('/\//', '_', $name);
        $name = preg_replace('/\./', '_', $name);

        return strtolower($name);
    }

    protected function getImages()
    {
        $files = Storage::disk('assets')->files('upload/placeholder');

        $files = array_map(function($file) {
            return '/' . $file;
        }, $files);

        return $files;
    }

    private function deleteCase($path)
    {
        if (is_dir($path)) {
            $dirs = scandir($path);

            foreach ($dirs as $dir) {
                //排除目录中的当前目录(.)和上一级目录(..)
                if ($dir != '.' && $dir != '..') {
                    //如果是目录则递归子目录，继续操作
                    $sonDir = rtrim($path, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . $dir;
                    if (is_dir($sonDir) && $dir !== $this->ignoreFolder) {
                        //递归删除
                        $this->deleteCase($sonDir);
                        //目录内的子目录和文件删除后删除空目录
                        @rmdir($sonDir);
                    } else {
                        //如果是文件直接删除
                        if (!in_array($dir, $this->ignoreFile)) {
                            @unlink($sonDir);
                        }
                    }
                }
            }

            //@rmdir($path);
        }
    }
}
